
function formateDate(dateStringInRange,timeInRange) {
    var isoExp = /^\s*(\d{4})-(\d\d)-(\d\d)\s*$/,
        date = new Date(NaN), month,
        parts = isoExp.exec(dateStringInRange);

    if(parts) {
        month = +parts[2];
        date.setFullYear(parts[1], month - 1, parts[3]);
        if(timeInRange && typeof timeInRange == "string"){
            var times = timeInRange.split(':');
            date.setHours(times[0],times[1],times[2])
        }
    }
    return date;
}
/**
 * 倒计时
 * @class Countdown
 * @extends Basic
 * @author 杜慧玲
 *
 *  @param  timeRemaining 到期时间
 *
 *  @param  tpl 倒计时显示的格式，默认：{d}天{h}时{m}分{s}秒
 *
 *  @param {function} overCallback 倒计时结束时回调函数
 *
 * 示例：
 *
 *     @example
 *     <Countdown tpl="{d}:{h}:{m}:{s}" timeRemaining="2017/09/01 00:00:00"/>
 */
var Countdown = React.createClass({
    getDefaultProps:function(){
        return{
            timeRemaining:"2017-04-01 0:0:0",
            tpl:"{d}天{h}时{m}分{s}秒",
            overCallback:function(){}
        }
    },
    start:function(){
        var _this = this;
        _this.countdownTime = setInterval(function(){
            _this._init();
        },1000);
    },
    stop:function(){
        clearInterval(this.countdownTime);
    },
    over:function(){
        this.stop();
        console.warn("时间已过");
        this.props.overCallback();
    },
    _init:function(){
        var _timeRemaining = this.props.timeRemaining;
        var _start = new Date(_timeRemaining).getTime();

        if(!_timeRemaining){
            _start = 0;
            this._countdown(0);
            return;
        }
        if(window.attachEvent && typeof _timeRemaining == "string" || isNaN(_start)){
            var timeArr = _timeRemaining.split(' ');
            _start = formateDate(timeArr[0],timeArr[1]).getTime();
        }
        var _now = new Date().getTime(),
            second = _start - _now;
        this._countdown(second);
    },
    _countdown:function(second){
        var _node = this.refs.countdown;
        var _this = this;
        if(second < 0){
            _this.over();
            return;
        }
        var _ts = second / 1000 / 60 / 60 % 24,
            _d = parseInt(second / 1000 / 60 / 60 / 24),    //得到天数
            _h = parseInt(_ts),      //得到小时
            _m = parseInt((_ts - _h) * 60),        //得到分钟
            _s = Math.floor(((_ts - _h) * 60 - _m) * 60);        //得到秒数

        var format = this.props.tpl, numArr = [], formatArr = [];
        if(format.indexOf("{d}") >= 0){
            numArr.push({
                value:_d,
                className:"ucs-count-day"
            });
        }
        if(format.indexOf("{h}") >= 0){
            numArr.push({
                value:_h,
                className:"ucs-count-hour"
            });
        }
        if(format.indexOf("{m}") >= 0){
            numArr.push({
                value:_m,
                className:"ucs-count-min"
            });
        }
        if(format.indexOf("{s}") >= 0){
            numArr.push({
                value:_s,
                className:"ucs-count-sec"
            });
        }

        format = format.replace("{d}","-");
        format = format.replace("{h}","-");
        format = format.replace("{m}","-");
        format = format.replace("{s}","-");
        formatArr = format.split('-');

        var innHtml = (
            <span>
                {
                    numArr.map(function(v,index){
                        var _v = v.value;
                        return <p><span className={v.className}>{_v}</span>{formatArr[(index+1)]}</p>
                    })
                }
            </span>
        )

        ReactDOM.render(innHtml,this.refs.countdown);
    },
    componentDidMount:function(){
        if(this.props.timeRemaining){
            this._init();
            this.start();
        }else{
            this._countdown(0);
        }
    },
    render:function(){
        return(
            <div ref="countdown" className="ucs-count">

            </div>
        )
    }
});
module.exports = Countdown;